package com.procedures;

import java.util.Date;

import java.util.HashMap;
import java.util.Properties;


import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import com.util.Component;


public class EmailServices {
	 com.PublicSystem sys=com.PublicSystem.getInstance();
//	public void sendMessage(HashMap map) throws Exception{
//		HtmlEmail email = new HtmlEmail();
//		
//		try {  
//			String authenusername = "sbjira@wondersgroup.com";
//
//			String authenpassword = "Wonders2009";
//
//			email.setHostName("smtp.wondersgroup.com");//邮件服务器  
//			email.setCharset("UTF-8");//编码格式  
//		
//			email.setAuthentication(authenusername, authenpassword);//smtp认证的用户名和密码
//			System.out.println("sql========================：select email,userzch,czsj from oa_usertable where id='"+(String) map.get("nextUserId")+"'");
//			
//			String toEmail[]=sys.SelResultNum("select email,userzch from oa_usertable where id='"+(String) map.get("nextUserId")+"'");
//			System.out.println("邮箱地址："+toEmail[0]);
//			if("".equals(toEmail[0])){return;}
//			else{
//				email.addTo(toEmail[0],toEmail[1]);//收信者  
//				
//				email.setFrom(authenusername, "njywgl");//发信者
//				
//				email.setSubject("关于："+map.get("title").toString()+"的任务分配待办提醒");//标题  
//				System.out.println("发送成功1==============================="+"关于："+map.get("title").toString()+"的任务分配待办提醒");
//				
//			
//				email.setMsg(map.get("task_ms").toString()+",申请时间为："+map.get("czsj").toString()+",请及时处理！");//内容  
//				System.out.println("发送成功2==============================="+map.get("task_ms").toString()+",<br>申请时间为："+map.get("czsj").toString()+",请及时处理！");
//				email.send();//发送  
//			}
//			System.out.println("发送成功===============================");
//		} catch (EmailException ex) {  
//			ex.printStackTrace();  
//		}  
//	}
	
	
	
	 	public boolean startSend(HashMap map) throws Exception
	    {
	 		
		    String toEmail[]=sys.SelResultNum("select email,userzch from oa_usertable where id='"+(String) map.get("nextUserId")+"'");

		    if("".equals(toEmail[0])){
		    
		    	return false;}
		    else
		    {
		    	
		    	String to = toEmail[0];
		    	String from = "sbjira@wondersgroup.com";
		    	String host = "smtp.wondersgroup.com";
		    
		    	String subject = "关于"+map.get("title").toString()+"的任务分配待办提醒";
		    	String text = map.get("task_ms").toString()+",提交时间为："+Component.systime_all()+",请及时处理！";
		    	String username = "sbjira";
		    	String password = "Wonders2009";
		    	
		    	
		   
		    	Properties props = System.getProperties();
		    	props.put("mail.smtp.host", host);
		    	props.put("mail.smtp.auth", "true");
		    	Session session = Session.getDefaultInstance(props, null);
		    	try
	        	{
		    		MimeMessage msg = new MimeMessage(session);
		    		msg.setFrom(new InternetAddress(from));
		    		InternetAddress address[] = {
		    				new InternetAddress(to)
		    		};
		    		msg.setRecipients(javax.mail.Message.RecipientType.TO, address);
		    		msg.setSubject(subject, "GB2312");
		    		Multipart mp = new MimeMultipart();
		    		MimeBodyPart mbpt = new MimeBodyPart();
		    		mbpt.setText(text, "GB2312");
		    		mp.addBodyPart(mbpt);
		    		MimeBodyPart mbp;
	           
		    		msg.setContent(mp);
		    		msg.setSentDate(new Date());
		    		Transport transport = session.getTransport("smtp");
		    		transport.connect(host, username, password);
		    		transport.sendMessage(msg, msg.getRecipients(javax.mail.Message.RecipientType.TO));
		    		transport.close();
	        	}
		    	catch(MessagingException mex)
		    	{
		    		mex.printStackTrace();
		    		Exception ex = null;
		    		if((ex = mex.getNextException()) != null)
		    			ex.printStackTrace();
		    		return false;
		    	}
		    	return true;
		    }
	    }
	
	
	
}
